﻿using Cinemachine;
using Memo.Electricity.Engine;
using Memo.Electricity.Setting;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Memo.Electricity.Game
{
    public class PlayerManager : MonoBehaviour
    {
        private static PlayerManager s_Instance;
        public static PlayerManager Inst
        {
            get
            {
                return s_Instance;
            }
        }

        private PlayerSetting setting;
        private GameObject player1;
        private GameObject player2;
        private bool isAlive = true;
        private float remainPower;
        [SerializeField]
        private KeyCode player1Left = KeyCode.LeftArrow;
        [SerializeField]
        private KeyCode player1Right = KeyCode.RightArrow;
        [SerializeField]
        private KeyCode player1Up = KeyCode.UpArrow;
        [SerializeField]
        private KeyCode player1Down = KeyCode.DownArrow;
        [SerializeField]
        private KeyCode player2Left = KeyCode.A;
        [SerializeField]
        private KeyCode player2Right = KeyCode.D;
        [SerializeField]
        private KeyCode player2Up = KeyCode.W;
        [SerializeField]
        private KeyCode player2Down = KeyCode.S;
        [SerializeField]
        private Vector3 player1StartPos;
        [SerializeField]
        private Vector3 player2StartPos;
        [SerializeField]
        private Sprite player1Sprite;
        [SerializeField]
        private Sprite player2Sprite;


        private void Awake()
        {
            setting = Resources.Load<PlayerSetting>("GameSetting/PlayerSetting");//有可能改动
            player1 = GameObject.Instantiate(setting.Player1, player1StartPos, transform.rotation);
            player2 = GameObject.Instantiate(setting.Player2, player2StartPos, transform.rotation);
            KeyAssignment();
            if (s_Instance != null)
                Debug.LogError("场景有两个PlayerManager");
            s_Instance = this;

            //初始化电流系统
            remainPower = setting.TotalPower;
            CRelay.Init();
        }
        private void KeyAssignment()//临时解决方案
        {
            player1.GetComponent<CharcterControlTest>().PlayerUp = player1Up;
            player1.GetComponent<CharcterControlTest>().PlayerDown = player1Down;
            player1.GetComponent<CharcterControlTest>().PlayerLeft = player1Left;
            player1.GetComponent<CharcterControlTest>().PlayerRight = player1Right;
            player2.GetComponent<CharcterControlTest>().PlayerUp = player2Up;
            player2.GetComponent<CharcterControlTest>().PlayerDown = player2Down;
            player2.GetComponent<CharcterControlTest>().PlayerLeft = player2Left;
            player2.GetComponent<CharcterControlTest>().PlayerRight = player2Right;
        }
        private void Update()
        {
            if (isAlive)
            {
                if (!CRelay.IsConnected)//两者距离过远，断电,此处条件应为是否断电
                    remainPower -= Time.deltaTime;
                else if (remainPower < setting.TotalPower)
                {
                    remainPower += Time.deltaTime;
                    if (remainPower >= setting.TotalPower)
                        remainPower = setting.TotalPower;
                }

                if (remainPower <= 0)
                {
                    CEventMgr.Inst.Broadcast(EEventType.OnPlayerDied);
                    CAudioMgr.Inst.PlayOneShot("Audios/dead");
                    isAlive = false;
                    Debug.Log("Game Over");
                }
            }
        }
        public Vector3 PlayerPos1
        {
            get => player1.transform.position;
        }
        public Vector3 PlayerPos2
        {
            get => player2.transform.position;
        }
        public Vector3 PlayerEle1
        {
            get => player1.transform.position + setting.OffSet;
        }
        public Vector3 PlayerEle2
        {
            get => player2.transform.position + setting.OffSet;
        }
        public GameObject Player1
        {
            get => player1;
        }
        public GameObject Player2
        {
            get => player2;
        }
        public float ElectricityLength
        {
            get => setting.ElectricityLength;
        }
        public float BatteryPer
        {
            get => remainPower * 1.0f / setting.TotalPower;
        }
        public PlayerSetting Setting
        {
            get => setting;
        }
        public Vector3 Player1StartPos
        {
            get => player1StartPos;
            set => player1StartPos = value;
        }

        public Vector3 Player2StartPos
        {
            get => player2StartPos;
            set => player2StartPos = value;
        }
        public bool IsAlive
        {
            get => isAlive;
        }

        public void PlayerDead()
        {
            remainPower = -10;
        }
        public void WinGame()
        {
            CEventMgr.Inst.Broadcast(EEventType.OnPlayerWin);
        }
    }
}
